package org.example;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Mybatis Plus持久层代码生成器
 * 直接运行然后按照控制台提示操作即可
 * <p>
 * Copyright @ 2022 Shanghai Mise Co. Ltd.
 * All right reserved.
 *
 * @author: LiuMingFu
 * @date: 2022/04/22 14:25
 */
public class MybatisPlusCodeGenerator {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/seata_account?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true";
        String username = "root";
        String password = "552200";
        FastAutoGenerator.create(url, username, password)
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？"))
                        .outputDir(scanner.apply("请指定输出目录？"))
                        // 覆盖已生成文件
//                        .fileOverride()
                        .dateType(DateType.TIME_PACK)
                        .enableSwagger()
                        .build())
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent("com.lmf.cloud"))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
//                        .addTablePrefix("t_smart_")
//                        .addFieldPrefix("fi_")
                        //控制器配置
                        .controllerBuilder()
                        .enableRestStyle()
                        .enableHyphenStyle()
                        //实体类配置：生成的实体如果JPA需要使用，请在实体上方增加@Entity注解，ID字段上方增加@Id注解
                        .entityBuilder()
//                        .superClass(BaseEntity.class)
                        .enableLombok()
                        .enableTableFieldAnnotation()
                        .idType(IdType.INPUT)
                        .addTableFills(
                                new Column("fi_create_time", FieldFill.INSERT)
                        )
                        .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                        //Service配置
                        .serviceBuilder()
                        .formatServiceFileName("%sService")
//                        .superServiceClass(ISmartBaseService.class)
                        .formatServiceImplFileName("%sServiceImpl")
//                        .superServiceImplClass(SmartBaseServiceImpl.class)
                        //Mapper配置
                        .mapperBuilder()
//                        .superClass(SmartBaseMapper.class)
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .build())
                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */
                .execute();
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
